package com.carrie.mdg.myviewtext.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.carrie.mdg.myviewtext.R;

import androidLib.activity.BaseActivity;

public class BroadcastTestActivity extends BaseActivity {
    private static final String TAG = "BroadcastTestActivity";

    private NetWorkChangeReceiver receiver;
    private IntentFilter intentFilter;

    private Button btnSendBroadcast;

    private LocalReceiver localReceiver;
    private LocalBroadcastManager localBroadcastManager;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, BroadcastTestActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_broadcast_test);
//        receiver = new NetWorkChangeReceiver();
//        IntentFilter filter = new IntentFilter();
//        filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
//        registerReceiver(receiver,filter);

        btnSendBroadcast = (Button) findViewById(R.id.button_send_broadcast);

        localBroadcastManager = LocalBroadcastManager.getInstance(this);

        intentFilter = new IntentFilter();
        intentFilter.addAction("com.carrie.broadcasttest.LOCAL_BROADCAST");
        localReceiver = new LocalReceiver();
        localBroadcastManager.registerReceiver(localReceiver, intentFilter);
    }

    @Override
    protected void initEvent() {
        btnSendBroadcast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent("com.carrie.broadcasttest.MY_BROADCAST");
////                sendBroadcast(intent);
//                sendOrderedBroadcast(intent,null);

                Intent intent = new Intent("com.carrie.broadcasttest.LOCAL_BROADCAST");
                localBroadcastManager.sendBroadcast(intent);//发送本地广播
            }
        });
    }

    @Override
    protected void loadData() {

    }

    @Override
    protected void dispose() {
        super.dispose();
//        unregisterReceiver(receiver);
        localBroadcastManager.unregisterReceiver(localReceiver);
    }

    class NetWorkChangeReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
//            Toast.makeText(context, "network has changed", Toast.LENGTH_SHORT).show();
            ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = manager.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isAvailable()) {
                Toast.makeText(context, "network is available", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "network is unavailable", Toast.LENGTH_SHORT).show();
            }
        }
    }

    class LocalReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "received local broadcast", Toast.LENGTH_SHORT).show();
        }
    }
}
